package cn.cocho.custom.yunCashierDf.merchant.service;

import cn.cocho.common.web.character.StringUtils;
import cn.cocho.common.web.jqgrid.JQGridPage;
import cn.cocho.common.web.jqgrid.JQGridUtils;
import cn.cocho.custom.yunCashierDf.merchant.domain.MerchantInfo;
import cn.cocho.dborm.core.Dborm;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by WQJ on 2017-05-23.
 */
@Service("merchantInfoService")
public class MerchantInfoService {

    public JQGridPage getListByJQgrid(JQGridPage<MerchantInfo> pageJQGrid) {
        pageJQGrid = JQGridUtils.getListByJQgrid(pageJQGrid, MerchantInfo.class, "merchant_info");
        for (MerchantInfo merchantInfo : pageJQGrid.getDataRows()) {
             merchantInfo.init();
        }
        return pageJQGrid;
    }

    public void update(MerchantInfo merchantInfo) {
        Dborm.replace(merchantInfo);
    }

    public void add(MerchantInfo merchantInfo) {
        merchantInfo.setMerchantId(StringUtils.getUUID());
        Dborm.insert(merchantInfo);
    }

    public MerchantInfo getMerchantInfoById(String merchanId) {
        MerchantInfo merchantInfo = new MerchantInfo();
        merchantInfo.setMerchantId(merchanId);
        return Dborm.getEntityByExample(merchantInfo, true);
    }

    public boolean insertMerchantInfo(MerchantInfo merchantInfo) {
        merchantInfo.setMerchantId(StringUtils.getUUID());
        return Dborm.insert(merchantInfo);
    }

    public boolean replaceMerchantInfo(MerchantInfo merchantInfo) {
        merchantInfo.setMerchantId(merchantInfo.getMerchantId());
        return Dborm.replace(merchantInfo);
    }

    public List<MerchantInfo> getMerchantInfo() {
        String sql = "SELECT * FROM merchant_info ORDER BY create_time DESC";
        return Dborm.getEntities(sql, null, MerchantInfo.class);
    }

}
